package com.dengwei.smali.first.dwpull_to_refresh.PullToRefresh;

import com.dengwei.smali.first.dwpull_to_refresh.PullToRefresh.indicator.PullToRereshIndicator;

/**
 * Created by dengwei on 2017/3/18.
 */

public class PullToRefreshHandlerHolder implements PullToRefreshUIHandler {

    private PullToRefreshUIHandler mHandler;
    private PullToRefreshHandlerHolder mNext;

    private boolean contains(PullToRefreshUIHandler handler) {
        return mHandler != null && mHandler == handler;
    }

    private PullToRefreshHandlerHolder() {
    }

    public boolean hasHandler() {
        return mHandler != null;
    }

    private PullToRefreshUIHandler getHandler() {
        return mHandler;
    }

    public static void addHandler(PullToRefreshHandlerHolder head, PullToRefreshUIHandler handler) {
        if (head == null || handler == null) return;

        if (head.mHandler == null){
            head.mHandler = handler;
            return ;
        }

        PullToRefreshHandlerHolder current = head;
        for (; ; current = current.mNext) {
            //复写
            if (current.contains(handler)) {
                return;
            }
            if (current.mNext == null) {
                break;
            }
        }

        PullToRefreshHandlerHolder newHolder = new PullToRefreshHandlerHolder();
        newHolder.mHandler = handler;
        current.mNext = newHolder;


    }

    public static PullToRefreshHandlerHolder create() {
        return new PullToRefreshHandlerHolder();
    }

    public static PullToRefreshHandlerHolder removeHandler(PullToRefreshHandlerHolder head, PullToRefreshUIHandler handler) {
        if (head == null || handler == null || head.mHandler == null) {
            return null;
        }
        PullToRefreshHandlerHolder current = head;
        PullToRefreshHandlerHolder pre = null;
        do {
            //删除当前
            //之前不会改变
            if (current.contains(handler)) {
                if (pre == null) {
                    head = current.mNext;
                    current.mNext = null;
                    current = head;
                } else {
                    pre.mNext = current.mNext;
                    current.mNext = null;
                    current = pre.mNext;
                }
            } else {
                pre = current;
                current = current.mNext;
            }
        } while (current != null);

        if (head == null) {
            head = new PullToRefreshHandlerHolder();
        }
        return head;
    }

    @Override
    public void onUIReset(PullToRefreshFrameLayout frame) {
        PullToRefreshHandlerHolder current = this;
        do {
            PullToRefreshUIHandler handler = current.getHandler();
            if (handler != null) {
                handler.onUIReset(frame);
            }
        } while ((current = current.mNext) != null);
    }

    @Override
    public void onUIRefreshPrepare(PullToRefreshFrameLayout frame) {
        if (!hasHandler()) return;
        PullToRefreshHandlerHolder current = this;
        do {
            PullToRefreshUIHandler handler = current.getHandler();
            if (handler != null) {
                handler.onUIRefreshPrepare(frame);
            }
        } while ((current = current.mNext) != null);
    }

    @Override
    public void onUIRefreshBegin(PullToRefreshFrameLayout frame) {
        PullToRefreshHandlerHolder current = this;
        do {
            PullToRefreshUIHandler handler = current.getHandler();
            if (handler != null) {
                handler.onUIRefreshBegin(frame);
            }
        } while ((current = current.mNext) != null);
    }

    @Override
    public void onUIRefreshComplete(PullToRefreshFrameLayout frame) {
        PullToRefreshHandlerHolder current = this;
        do {
            PullToRefreshUIHandler handler = current.getHandler();
            if (handler != null) {
                handler.onUIRefreshComplete(frame);
            }
        } while ((current = current.mNext) != null);
    }

    @Override
    public void onUIPositionChange(PullToRefreshFrameLayout frame, boolean isUnderTouch, int status, PullToRereshIndicator indicator) {
        PullToRefreshHandlerHolder current = this;
        do {
            PullToRefreshUIHandler handler = current.getHandler();
            if (handler != null) {
                handler.onUIPositionChange(frame, isUnderTouch, status, indicator);
            }
        } while ((current = current.mNext) != null);
    }
}
